package cn.yunhe.biz.impl;

import cn.yunhe.biz.AccountBiz;
import cn.yunhe.dao.AccountDao;
import cn.yunhe.dao.RecordDao;
import cn.yunhe.pojo.Account;
import cn.yunhe.pojo.Record;

import java.util.List;

public class AccountBizImpl implements AccountBiz {
    AccountDao accountDao;
    RecordDao recordDao;
    @Override
    public boolean insertAccount(Account account) {
        return accountDao.insertAccount(account);
    }

    @Override
    public boolean updateAccount(Account account) {
        return accountDao.updateAccount(account);
    }

    @Override
    public boolean deleteAccount(int id) {
        return accountDao.deleteAccount(id);
    }

    @Override
    public Account getById(int id) {
        return accountDao.getById(id);
    }

    @Override
    public List<Account> findAccount(String hql) {
        return accountDao.findAccount(hql);
    }

    @Override
    public List<Account> findAccount(Account account) {
        return accountDao.findAccount(account);
    }

    @Override
    public boolean doTransferAccount(int fromaid, int toaid, Double ftMoney) {
        Account fromAccount=accountDao.getById(fromaid);
        Account toAccount=accountDao.getById(toaid);

        /*try{*/
            /*if (ftMoney>0){
                if (fromAccount.getAmoney()>0){*/
        fromAccount.setAmoney(fromAccount.getAmoney()-ftMoney);
        accountDao.updateAccount(fromAccount);
        toAccount.setAmoney(toAccount.getAmoney()+ftMoney);
        accountDao.updateAccount(toAccount);

        /*RecordDao recordDao=new RecordDaoImpl();*/
        Record record2new=new Record();
        record2new.setRmoney(ftMoney);
        record2new.setRdate("2018-08-29");
        record2new.setRmark("同事转账1");
        record2new.setFromAccount(fromAccount);
        record2new.setToAccount(toAccount);
        boolean flag=recordDao.insertRecord(record2new);
               /* }
            }*/
       /* }catch (Exception e){
            throw new RuntimeException();
        }*/

        return false;
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public RecordDao getRecordDao() {
        return recordDao;
    }

    public void setRecordDao(RecordDao recordDao) {
        this.recordDao = recordDao;
    }
}
